package com.wuze.TCC.Processor;




import com.wuze.TCC.annotation.TCC;
import com.wuze.TCC.proxy.CglibProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;



/**
 * 处理器
 * 使用@Import(Processor.class)导入
 * @author wuze
 */
@Component
public class Processor implements BeanPostProcessor {


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (Method method : methods) {
            TCC annotation = method.getAnnotation(TCC.class);
            if (annotation != null){
                System.out.println(beanName+"设置代理");
                Object proxyObj = CglibProxyFactory.creatCglibProxyObj(bean,bean.getClass());
                return proxyObj;
            }
        }
        return bean;
    }

}
